package com.song.module.admin.controller;


import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import com.song.common.enums.BusinessType;
import com.song.common.result.R;
import com.song.common.util.ExcelUtils;
import io.swagger.annotations.Api;
import com.song.framework.annotation.RateLimiter;
import com.song.module.admin.entity.ApCollect;
import com.song.framework.annotation.Log;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import com.song.common.response.PageInfo;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import com.song.module.admin.service.IApCollectService;
import com.song.module.admin.request.ApCollectRequest;

import javax.servlet.http.HttpServletResponse;

/**
 * 收藏控制层
 *
 */
@Slf4j
@AllArgsConstructor
@RestController
@Api(value = "收藏接口管理",tags = "收藏接口管理")
@RequestMapping("/admin/apCollect" )
public class ApCollectController {

    private final IApCollectService iApCollectService;

    /**
     * 分页查询收藏列表
     */
    @ApiOperation(value = "分页查询收藏列表")
    @PostMapping("/pageList")
    public R<PageInfo<ApCollect>> pageList(@RequestBody ApCollectRequest apCollectRequest) {
        return R.success(iApCollectService.pageList(apCollectRequest));
    }

    /**
     * 获取收藏详细信息
     */
    @ApiOperation(value = "获取收藏详细信息")
    @GetMapping(value = "/query/{id}")
    public R<ApCollect> detail(@PathVariable("id") Long id) {
        return R.success(iApCollectService.getById(id));
    }

    /**
     * 新增收藏
     */
    @Log(title = "新增收藏", businessType = BusinessType.INSERT)
    @RateLimiter
    @ApiOperation(value = "新增收藏")
    @PostMapping("/add")
    public R<Void> add(@RequestBody ApCollectRequest apCollectRequest) {
        iApCollectService.add(apCollectRequest);
        return R.success();
    }

    /**
     * 修改收藏
     */
    @Log(title = "修改收藏", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改收藏")
    @PostMapping("/update")
    public R<Void> edit(@RequestBody ApCollectRequest apCollectRequest) {
        iApCollectService.update(apCollectRequest);
        return R.success();
    }

    /**
     * 删除收藏
     */
    @Log(title = "删除收藏", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除收藏")
    @PostMapping("/delete/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        iApCollectService.deleteBatch(Arrays.asList(ids));
        return R.success();
    }

    /**
    * 导出收藏
    *
    * @param response response
    * @throws IOException IOException
    */
    @ApiOperation(value = "导出收藏")
    @PostMapping(value = "/export")
    public void exportExcel(HttpServletResponse response,@RequestBody ApCollectRequest apCollectRequest) throws IOException {
        List<ApCollect> list = iApCollectService.list(apCollectRequest);
        ExcelUtils.exportExcel(list, ApCollect.class, "收藏信息", response);
    }
}
